package cn.wolfcode.wolf2w.search.service.impl;

import cn.wolfcode.wolf2w.search.api.domain.NoteEs;
import cn.wolfcode.wolf2w.common.core.constant.SecurityConstants;
import cn.wolfcode.wolf2w.common.core.domain.R;
import cn.wolfcode.wolf2w.common.core.utils.StringUtils;
import cn.wolfcode.wolf2w.note.api.RemoteNoteService;
import cn.wolfcode.wolf2w.note.api.domain.Note;
import cn.wolfcode.wolf2w.search.query.SearchQueryObject;
import cn.wolfcode.wolf2w.search.repository.NoteEsRepository;
import cn.wolfcode.wolf2w.search.service.INoteEsService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.elasticsearch.core.SearchPage;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class NoteEsServiceImpl implements INoteEsService {

    @Autowired
    private NoteEsRepository repository;

    @Autowired
    private RemoteNoteService remoteNoteService;

    @Override
    public void save(NoteEs note) {
        repository.save(note);
    }

    @Override
    public void update(NoteEs note) {
        repository.save(note);
    }

    @Override
    public NoteEs get(String id) {
        return repository.findById(id).orElse(null);
    }

    @Override
    public List<NoteEs> list() {
        List<NoteEs> list = new ArrayList<>();
        Iterable<NoteEs> all = repository.findAll();
        all.forEach(a -> list.add(a));
        return list;
    }

    @Override
    public void delete(String id) {
        repository.deleteById(id);
    }

    @Override
    public Page<Note> queryByKeyWord(SearchQueryObject qo) {
        String keyword = qo.getKeyword();
        PageRequest pageRequest = PageRequest.of(qo.getCurrentPage() - 1, qo.getPageSize());

        SearchPage<NoteEs> searchPage = repository.queryByTitleOrSummary(keyword, keyword, pageRequest);

        List<Note> list = searchPage.getSearchHits().stream().map(searchHit -> {
            R<Note> ret = remoteNoteService.getOne(Long.parseLong(searchHit.getId()), SecurityConstants.INNER);
            Note data = ret.getData();
            StringUtils.handleESHighlight(searchHit.getHighlightFields(), data);
            return data;
        }).collect(Collectors.toList());

        return new PageImpl<>(list, pageRequest, searchPage.getTotalElements());
    }

    @Override
    public void initNoteEs() {
        R<List<Note>> result = remoteNoteService.list(SecurityConstants.INNER);
        List<Note> list = result.getData();

        List<NoteEs> esList = list.stream().map(record -> {
            NoteEs travelEs = new NoteEs();
            BeanUtils.copyProperties(record, travelEs);
            return travelEs;
        }).collect(Collectors.toList());

        repository.saveAll(esList);
    }


}
